Attribute VB_Name = "modWarmup"
Option Explicit

Public Enum MX2WarmupTableType
    mx2tbltyp50kV4W
    mx2tbltyp50kV10W
    mx2tbltyp70kV10W
    mx2tbltypCustom
End Enum

Public Type MiniX2WarmUpTable
    varWarmUpVoltage As Variant
    varWarmUpCurrent As Variant
    varWarmUpTime As Variant
    WarmUpTableType As MX2WarmupTableType
    DailyWarmupTotalTime As Long
    MonthlyWarmupTotalTime As Long
End Type

Public WarmUpTable As MiniX2WarmUpTable
Public Const DailyWarmupIndexStart As Long = 0
Public Const MonthlyWarmupIndexStart As Long = 6

Public Function CalcWarmupTotalTime(varWarmUpTable As Variant, WarmupIndexStart As Long, Optional NumberWarmupIndexes As Long = 6) As Single
    Dim idxWarmup As Long
    Dim sngTotal As Single
    CalcWarmupTotalTime = 0#
    sngTotal = 0
    If (IsArray(varWarmUpTable)) Then
        For idxWarmup = WarmupIndexStart To ((WarmupIndexStart + NumberWarmupIndexes) - 1)
            sngTotal = sngTotal + CSng(Val(varWarmUpTable(idxWarmup)))
        Next
    End If
    CalcWarmupTotalTime = sngTotal
End Function

Public Function Process_MNX_Warmup_Table(PIN As Packet_In, ByRef WarmUpTable As MiniX2WarmUpTable, Optional WarmUpTableType As MX2WarmupTableType = mx2tbltyp50kV10W) As String
    Dim idxWUEntry As Long
    Dim strTable As String
    Dim lWup As Long
    Dim strErrorHelp As String
    
    On Error GoTo Process_MNX_Warmup_TableErr
    
    Process_MNX_Warmup_Table = ""
    
    strErrorHelp = "Checking WarmUpTable Struct"
    'the table values are stored in variant arrays to allow quick array init
    If (Not IsArray(WarmUpTable.varWarmUpTime)) Then
        strErrorHelp = "WarmUpTable Struct needs init"
        Select Case WarmUpTableType
            Case mx2tbltyp50kV10W
                strErrorHelp = "Create50kV10W_WarmUpTable WarmUpTable"
                Create50kV10W_WarmUpTable WarmUpTable
            Case mx2tbltyp70kV10W
                strErrorHelp = "Create70kV10W_WarmUpTable WarmUpTable"
                Create70kV10W_WarmUpTable WarmUpTable
            Case mx2tbltyp50kV4W
                strErrorHelp = "Create50kV4W_WarmUpTable WarmUpTable"
                Create50kV4W_WarmUpTable WarmUpTable
            Case mx2tbltypCustom
                strErrorHelp = "Create50kV10W_WarmUpTable WarmUpTable"
                Create50kV10W_WarmUpTable WarmUpTable
            Case Else
                strErrorHelp = "Not initialized"
        End Select
        strErrorHelp = "WarmUpTable Struct init done"
    End If
    
    strErrorHelp = "Loading WarmUpTable data from PIN"
    For idxWUEntry = 0 To 11
        WarmUpTable.varWarmUpVoltage(idxWUEntry) = CSng(Val(PIN.Data(idxWUEntry * 4)))
        WarmUpTable.varWarmUpCurrent(idxWUEntry) = CSng(Val(PIN.Data(idxWUEntry * 4 + 1)))
        WarmUpTable.varWarmUpTime(idxWUEntry) = CSng(CLng(PIN.Data(idxWUEntry * 4 + 2)) * 256 + CLng(PIN.Data(idxWUEntry * 4 + 3)))
    Next idxWUEntry
    strErrorHelp = "Set Warmup type (if any)"
    WarmUpTable.WarmUpTableType = WarmUpTableType
    strErrorHelp = "Calc daily warmup total"
    WarmUpTable.DailyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, DailyWarmupIndexStart)
    strErrorHelp = "Calc monthly warmup total"
    WarmUpTable.MonthlyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, MonthlyWarmupIndexStart)

    strTable = ""
    strTable = strTable + "======= Warmup Table ===========" + vbCrLf
    strErrorHelp = "Creating Warmup Table Display"
    For idxWUEntry = 0 To 11
        If (idxWUEntry < 6) Then
            strTable = strTable + "Daily Warmup Step #" + Format(idxWUEntry + 1, "#0") + ": "
        Else
            strTable = strTable + "Monthly Warmup Step #" + Format(idxWUEntry - 5, "#0") + ": "
        End If
        strTable = strTable + Format(WarmUpTable.varWarmUpVoltage(idxWUEntry), "##0kV") + ", "
        strTable = strTable + Format(WarmUpTable.varWarmUpCurrent(idxWUEntry), "##0uA") + ", "
        strTable = strTable + Format(WarmUpTable.varWarmUpTime(idxWUEntry), "###0") + "sec" + vbCrLf
        If (idxWUEntry = 5) Then
            lWup = CLng(WarmUpTable.DailyWarmupTotalTime)
            'MsgBox lWup
            strTable = strTable + "Daily Warmup Total time = " + Format(lWup \ 60, "#0m ") + Format(lWup Mod 60, "00s") + vbCrLf
        ElseIf (idxWUEntry = 11) Then
            lWup = CLng(WarmUpTable.MonthlyWarmupTotalTime)
            'MsgBox lWup
            strTable = strTable + "Monthly Warmup Total time = " + Format(lWup \ 60, "#0m ") + Format(lWup Mod 60, "00s") + vbCrLf
        End If
    Next
    Process_MNX_Warmup_Table = strTable
    Exit Function
    
Process_MNX_Warmup_TableErr:
    MsgBox Err.Description & " --> " & strErrorHelp
End Function

Public Sub Create50kV10W_WarmUpTable(ByRef WarmUpTable As MiniX2WarmUpTable)
' 50kV10W Daily warm-up 0-5,Monthly warm-up 6-11
    WarmUpTable.varWarmUpVoltage = Array(25!, 30!, 40!, 40!, 50!, 20!, 25!, 30!, 40!, 40!, 50!, 20!)
    WarmUpTable.varWarmUpCurrent = Array(20!, 50!, 50!, 100!, 200!, 200!, 20!, 50!, 50!, 100!, 200!, 200!)
    WarmUpTable.varWarmUpTime = Array(5!, 5!, 5!, 5!, 5!, 5!, 300!, 300!, 300!, 600!, 600!, 600!)
    WarmUpTable.WarmUpTableType = MX2WarmupTableType.mx2tbltyp50kV10W
    WarmUpTable.DailyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, DailyWarmupIndexStart)
    WarmUpTable.MonthlyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, MonthlyWarmupIndexStart)
End Sub

Public Sub Create50kV4W_WarmUpTable(ByRef WarmUpTable As MiniX2WarmUpTable)
' 50kV4W Daily warm-up 0-5,Monthly warm-up 6-11
    WarmUpTable.varWarmUpVoltage = Array(25!, 30!, 40!, 40!, 50!, 20!, 25!, 30!, 40!, 40!, 50!, 20!)
    WarmUpTable.varWarmUpCurrent = Array(20!, 50!, 50!, 100!, 80!, 200!, 20!, 50!, 50!, 100!, 80!, 200!)
    WarmUpTable.varWarmUpTime = Array(5!, 5!, 5!, 5!, 5!, 5!, 300!, 300!, 300!, 600!, 600!, 600!)
    WarmUpTable.WarmUpTableType = MX2WarmupTableType.mx2tbltyp50kV4W
    WarmUpTable.DailyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, DailyWarmupIndexStart)
    WarmUpTable.MonthlyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, MonthlyWarmupIndexStart)
End Sub

Public Sub Create70kV10W_WarmUpTable(ByRef WarmUpTable As MiniX2WarmUpTable)
' 70kV10W Daily warm-up 0-5,Monthly warm-up 6-11
    WarmUpTable.varWarmUpVoltage = Array(10!, 35!, 50!, 50!, 60!, 70!, 10!, 25!, 40!, 50!, 50!, 60!)
    WarmUpTable.varWarmUpCurrent = Array(20!, 20!, 100!, 200!, 166!, 143!, 50!, 50!, 50!, 100!, 200!, 266!)
    WarmUpTable.varWarmUpTime = Array(5!, 5!, 5!, 5!, 5!, 5!, 300!, 300!, 300!, 300!, 300!, 300!)
    WarmUpTable.WarmUpTableType = MX2WarmupTableType.mx2tbltyp70kV10W
    WarmUpTable.DailyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, DailyWarmupIndexStart)
    WarmUpTable.MonthlyWarmupTotalTime = CalcWarmupTotalTime(WarmUpTable.varWarmUpTime, MonthlyWarmupIndexStart)
End Sub

